<?php
	//include("../csdl.php");
	//$knoi=new csdl();
	$makh=$_POST['khachhang'];
	$anhtm=$_POST['anhtm'];
	$anldm=$_POST['anldm'];
	$tomluoc-$_POST['tomluoc'];
	$nguoidang=$_SESSION['admin'];
	if($anhtm==1)
	{
		$maht=getCode('hinhthuc','maht',2);	
		echo $maht;
		$tenht=$_POST['tenhtmoi'];
		$knoi->queryCsdl("insert into hinhthuc values('$maht','$tenht')");
	}
	else
		$maht=$_POST['hinhthuc'];
	if($anldm==1)
	{
		$maloai=$knoi->getCode('loainhadat','maloai',2);	
		echo $maloai;
		$tenlnd=$_POST['tenlndmoi'];
		$knoi->queryCsdl("insert into loainhadat values('$maloai','$tenlnd')");
		echo $maloai;
	}
	else
		$maloai=$_POST['loainhadat'];
	//$loaitin=$_POST['loaitin'];
	$tinh=$_POST['tinh']; //xử lý tỉnh auto complete
	$kttinh=$knoi->queryCsdl("select mattp from tinhthanhpho where tentinhtp='$tinh'");
	if(mysql_num_rows($kttinh)==1)
	{
		$layma=mysql_fetch_array($kttinh);
		$mattp=$layma['mattp'];
	}
	else
	{
		$mattp=$knoi->getCode("tinhthanhpho","mattp",2);
		$knoi->queryCsdl("insert into tinhthanhpho values('$mattp','$tinh')");
	}
	$matin=$knoi->getCode('tinraovat','matin',0);
	//echo "<br>".$mattp;
	$tieude=$_POST['tieude'];
	$noidung=$_POST['noidung'];
	$dientich=$_POST['dientich'];
	$gia=$_POST['gia'];
	$donvi=$_POST['donvi'];
	$anh=$_POST['anh'];
	$ngaydang=$_POST['tungay'];
	$ngaydang=str_replace('/','-',$ngaydang);
	$ngaydang=$knoi->formatDate($ngaydang);
	//echo $ngaydang."<br>";
	$ngayhh=$_POST['denngay'];
	$ngayhh=str_replace('/','-',$ngayhh);
	$ngayhh=$knoi->formatDate($ngayhh);
	$diachi=$_POST['diachi'];
	$path_thumbs = "upload_images";
			//the new width of the resized image, in pixels.
			$img_thumb_width = 100; // 
	
			$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
			//List of allowed extensions if extlimit = yes
			$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
			//echo "<br>i: ".$i."<br>";
			$tam=basename( $_FILES['anh']['name']);
			//echo "fgdk".$tam;
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$result='';
			$resize='';
			//echo "<br>".$_FILES['anh'.$i]['name'];
			$duongdan ="upload_images\/".$tam;
			//the image -> variables
			$file_type = $_FILES['anh']['type'];
			$file_name = $_FILES['anh']['name'];
			$file_size = $_FILES['anh']['size'];
			$file_tmp = $_FILES['anh']['tmp_name'];
			$ext = strrchr($file_name,'.');
		   $ext = strtolower($ext);
		   /*if (($extlimit == "yes") && (!in_array($ext,$limitedext))) 
		   {
			  echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
			  exit();
		   }*/
		   //so, whats the file's extension?
		   $getExt = explode ('.', $file_name);
		   $file_ext = $getExt[count($getExt)-1];
		   $rand_name = md5(time());
		   $rand_name= rand(0,999999999);
		   $ThumbWidth = $img_thumb_width;
		   if($file_size)
		   {
			  if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
			  {
				   $new_img = imagecreatefromjpeg($file_tmp);
			   }
			   elseif($file_type == "image/x-png" || $file_type == "image/png")
			   {
				   $new_img = imagecreatefrompng($file_tmp);
			   }
			   elseif($file_type == "image/gif")
			   {
				   $new_img = imagecreatefromgif($file_tmp);
			   }
			   //list the width and height and keep the height ratio.
			   list($width, $height) = getimagesize($file_tmp);
			   //calculate the image ratio
			   $imgratio=$width/$height;
			   if ($imgratio>1)
			   {
				  $newwidth = $ThumbWidth;
				  $newheight = $ThumbWidth/$imgratio;
			   }
			   else
			   {
					 $newheight = $ThumbWidth;
					 $newwidth = $ThumbWidth*$imgratio;
			   }
			   //function for resize image.
			   if (function_exists(imagecreatetruecolor))
			   {
			   $resized_img = imagecreatetruecolor($newwidth,$newheight);
			   }
			   else
			   {
					 die("Error: Please make sure you have GD library ver 2+");
			   }
			   //the resizing is going on here!
			   imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			   //finally, save the image
			   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
			   ImageDestroy ($resized_img);
			   ImageDestroy ($new_img);
			}
			if(@move_uploaded_file($_FILES['anh']['tmp_name'], $duongdan)) 
		   {
		   		@move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");
			  $result = $duongdan;
			  $resize="$path_thumbs/$rand_name.$file_ext";
			  $knoi->queryCsdl("insert into hinhanh values('','$result','$resize','','$matt')");
		   }
	//echo "tinh: ".$tinh.", tieude: ".$tieude.", noidung: ".$noidung;
	if($knoi->queryCsdl("insert into tinraovat values('$matin','$tieude','$tomluoc','$noidung','$ngaydang','$ngayhh','$dientich','$gia','$donvi','$diachi','$result','$resize','$maloai','$maht','$mattp','$makh','$nguoidang')"))
	{
	?>
		<script language="javascript">alert("Đã thêm tin rao vặt thành công!");</script>
	<?php
		echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=danhsachtinraovat'/>";
	}
	else
	{
		//$ngaydang=$knoi->formatDate($ngaydang);
		//$ngayhh=$knoi->formatDate($ngayhh);
	?>
		<script language="javascript">alert("Có lỗi xãy ra. Xin vui lòng kiểm tra lại!");</script>
	<?php
		echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=tinraovat&tieude=".$tieude."&noidung=".$noidung."&dientich=".$dientich."&gia=".$gia."&donvi=".$donvi."&ngaydang=".$ngaydang."&ngayhethan=".$ngayhh."&diachi=".$diachi."&makh=".$makh."&maht=".$maht."&maloai=".$maloai."&tinh=".$tinh."&tomluoc=".$tomluoc."'/>";
	}
?>
